
#include "Data/User.h"

#include "NewsManager.h"

namespace TC
{
	namespace Request
	{
		Q_INVOKABLE void NewsManager::add(quint64 groupId, const QVariantMap &news)
		{
			if (status() == Status::Communicating)
				return;
			QVariantMap args = news;
			args.insert("groupId", groupId);
			args.insert("signature", Data::User::instance()->signature());
			AbstractRemoteProcedure::get("discover/news/add", args);
		}

		Q_INVOKABLE void NewsManager::update(const QVariantMap &news)
		{
			if (status() == Status::Communicating)
				return;
			QVariantMap args = news;
			args.insert("signature", Data::User::instance()->signature());
			AbstractRemoteProcedure::get("discover/news/update", args);
		}

		Q_INVOKABLE void NewsManager::remove(quint64 id)
		{
			if (status() == Status::Communicating)
				return;
			AbstractRemoteProcedure::get("discover/news/remove",
			{
				{ "signature", Data::User::instance()->signature() },
				{ "id", id },
			});
		}

		void NewsManager::onDataReady(QDataStream &stream)
		{
			bool result;
			stream >> result;
			if (result)
			{
				setOk();
			}
			else
			{
				setFailed();
			}
		}

	}
}